package com.ayf.payment.game.api.controller.platform;

import com.ayf.payment.game.api.dto.PageDTO;
import com.ayf.payment.game.api.dto.platform.SubstitutePayConfigDTO;
import com.ayf.payment.game.api.dto.platform.criteria.SubstitutePayCriteria;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.platform.SubstitutePayConfigService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.type.PayChannelCodeEnum;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/t/pay")
@RestController
public class SubstitutePayConfigController {

    @Autowired
    SubstitutePayConfigService substitutePayConfigService;

    /**
     * 获取代付设置
     *
     * @return
     */
    @PostMapping("/getConfig")
    public CommonResult<PageDTO> getPayConfig(@RequestBody SubstitutePayCriteria substitutePayCriteria) {
        PageHelper.startPage(substitutePayCriteria.getPageNum(), substitutePayCriteria.getPageSize());
        List<SubstitutePayConfigDTO> list =
                substitutePayConfigService.getListSubstitutePayConfigByChannelCode(substitutePayCriteria);
        PageDTO dto = new PageDTO();
        if (list != null && list.size() > 0) {
            PageInfo<SubstitutePayConfigDTO> pageInfo = new PageInfo<>(list);
            dto = BeanConvertUtils.convert(pageInfo, PageDTO.class);
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", dto);
    }

    /**
     * 更新代付信息
     *
     * @return
     */
    @PostMapping("/updateConfig")
    public CommonResult<Object> updateConfig(@RequestBody SubstitutePayConfigDTO substitutePayConfigDTO) {
        if (substitutePayConfigDTO.getId() == null) {
            throw new BusinessException("代付渠道id不能为空");
        }
        if (substitutePayConfigService.update(substitutePayConfigDTO)) {
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "更新代付信息成功");
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "更新代付信息失败");
    }

    /**
     * 更新代付通道参数配置
     *
     * @return
     */
    @PostMapping("/updatePayConfig")
    public CommonResult<?> updatePayConfig(@RequestBody SubstitutePayConfigDTO substitutePayConfigDTO) {
        if (substitutePayConfigDTO.getId() == null) {
            throw new ParameterException("代付通道主键ID不能为空");
        }
        if (StringUtils.isEmpty(substitutePayConfigDTO.getChannelCode())) {
            throw new ParameterException("代付通道code不能为空");
        }
        if (PayChannelCodeEnum.EHB_PAY.getCode().equals(substitutePayConfigDTO.getChannelCode())) {
            if (substitutePayConfigDTO.getEhbPayConfigParamVO() == null) {
                throw new ParameterException("支付通道参数不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getEhbPayConfigParamVO().getChannelMerchantId())) {
                throw new ParameterException("支付通道商户号不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getEhbPayConfigParamVO().getChannelUrl())) {
                throw new ParameterException("支付通道接口地址不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getEhbPayConfigParamVO().getChannelPubKey())) {
                throw new ParameterException("支付通道公钥不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getEhbPayConfigParamVO().getChannelSecretKey())) {
                throw new ParameterException("支付通道密钥不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getEhbPayConfigParamVO().getMerchantPriKey())) {
                throw new ParameterException("商户私钥不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getEhbPayConfigParamVO().getMerchantPubKey())) {
                throw new ParameterException("商户公钥不能为空");
            }
        } else {
            if (StringUtils.isEmpty(substitutePayConfigDTO.getWanXiangPayConfigParamVO().getWebsite())) {
                throw new ParameterException("url不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getWanXiangPayConfigParamVO().getKey())) {
                throw new ParameterException("key不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getWanXiangPayConfigParamVO().getParter())) {
                throw new ParameterException("商户id不能为空");
            }
            if (StringUtils.isEmpty(substitutePayConfigDTO.getWanXiangPayConfigParamVO().getNotifyurl())) {
                throw new ParameterException("异步通知地址不能为空");
            }
        }
        substitutePayConfigService.updateSubstitutePayConfig(substitutePayConfigDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "保存成功");
    }

    /**
     * 更新易惠宝商户代付公私钥对
     * @param substitutePayConfigDTO
     * @return
     */
    @PostMapping("/updateEhbPayConfigMerchantPubPriKey")
    public CommonResult updateEhbPayConfigMerchantPubPriKey(@RequestBody SubstitutePayConfigDTO substitutePayConfigDTO) {
        if(substitutePayConfigDTO==null){
            throw new ParameterException("参数不能为空");
        }
        if(substitutePayConfigDTO.getId() == null){
            throw new ParameterException("代付通道主键ID不能为空");
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "更新成功", substitutePayConfigService.updateSubstitutePayConfigMerchantPubPriKey(substitutePayConfigDTO));
    }
}
